#include <assert.h>

#include "set.h"

#include "set_errno.h"

#define INT__SIZE 32

struct set set__empty()
{
  struct set tmp;
  int i = 0;

  for(i = 0; i < SET__SIZE;i++)
    tmp.bits[i] = 0;
  return tmp;
}


size_t set__size(const struct set* array)
{
  int i = 0;
  unsigned int temp = 0;
  size_t count = 0;
  
  for(i = 0; i < SET__SIZE; i++)
    {
      temp = array->bits[i];
      while(temp)
	{
	  count ++;
	  temp ^= (temp & -temp); //BEWARE !!
	}
    }
  return count;
}

 
int set__add(struct set* array, char c)
{
    unsigned char uc = (unsigned char)c;
    int i = 0;
    int index = uc / INT__SIZE;
    int bit_mask = 1<< (uc % INT__SIZE);

    array->bits[index] |= bit_mask;
    return 0;
}


int set__remove(struct set* array, char c)
{
  unsigned char uc = (unsigned char)c;
  int index = uc / INT__SIZE;
  int bit_mask = ~(1 << (uc % INT__SIZE));
  
  array->bits[index] &= bit_mask;
  return 0;
}


int set__is_empty(const struct set* array)
{
  int i = 0;
  
  for(i = 0; i< SET__SIZE; i++)
    if(array->bits[i])
      return 0;
  return 1;
}


int set__find(const struct set* array, char c)
{
  int i = 0;
  int index = c / INT__SIZE;
  int bit_number = c % INT__SIZE;
  int temp = array->bits[index];
  
  temp = temp >> bit_number;
  return temp % 2;
}


int set__inter(const struct set * s1, const struct set * s2, struct set * dst)
{
  int i = 0;
  for(i = 0; i < SET__SIZE; i++)
    dst->bits[i] = s1->bits[i] & s2->bits[i];
  return 0;
}


int set__union(const struct set * s1, const struct set * s2, struct set * dst)
{
  int i = 0;
  for(i = 0; i < SET__SIZE; i++)
    dst->bits[i] = s1->bits[i] | s2->bits[i];
  return 0;
}

